package pl.edu.utp.kursy;

import javax.swing.JOptionPane;
import pl.edu.utp.kursy.database.Company;

public class CompanyInfoPanel extends javax.swing.JPanel {
    
    private Company company;
    
    public CompanyInfoPanel(Company company) {
        this.company = company;
        initComponents();
        setCompanyFields();
        setEditableMode(false);
    }

    public Company getCompany() {
        
        if(validateFields()){
            if(company == null){
                company = new Company();
            }

            company.setName(companyNameTextField.getText());
            company.setAddress(companyAddressTextField.getText());
            company.setCity(companyCityTextField.getText());
            company.setZipCode(companyZipCodeTextField.getText());
            company.setEmail(companyEmailTextField.getText());
            company.setFax(companyFaxTextField.getText());
            company.setNip(companyNipTextField.getText());
            company.setPhone(companyPhoneTextField.getText());
            company.setRegon(companyRegonTextField.getText());
            return company;
        }
        
        return null;
    }
    
    private boolean validateFields(){
        
        String name = companyNameTextField.getText();
        String address = companyAddressTextField.getText();
        String city = companyCityTextField.getText();
        String zipCode = companyZipCodeTextField.getText();
        String email = companyEmailTextField.getText();
        String fax = companyFaxTextField.getText();
        String nip = companyNipTextField.getText();
        String phone = companyPhoneTextField.getText();
        String regon = companyRegonTextField.getText();
        
        if(name.isEmpty()){
            JOptionPane.showMessageDialog(this, getString("company_name_empty_message"),
                                                getString("company_name_empty_title"),
                                                JOptionPane.WARNING_MESSAGE);
            return false;
        }
        
        //TODO Dodać walidacje peselu, nipu itp, chociaz nie wiem czy sobei nei darować.
        
        return true;
    }

    public void setCompany(Company company) {
        this.company = company;
        setCompanyFields();
        setEditableMode(false);
    }
    
    private void setCompanyFields(){
        if(company != null){
            companyAddressTextField.setText(company.getAddress());
            companyCityTextField.setText(company.getCity());
            companyEmailTextField.setText(company.getEmail());
            companyFaxTextField.setText(company.getFax());
            companyNameTextField.setText(company.getName());
            companyNipTextField.setText(company.getNip());
            companyPhoneTextField.setText(company.getCity());
            companyRegonTextField.setText(company.getRegon());
            companyZipCodeTextField.setText(company.getZipCode());
        }
    }
    
    public final void setEditableMode(boolean isEditable){
            companyAddressTextField.setEditable(isEditable);
            companyCityTextField.setEditable(isEditable);
            companyEmailTextField.setEditable(isEditable);
            companyFaxTextField.setEditable(isEditable);
            companyNameTextField.setEditable(isEditable);
            companyNipTextField.setEditable(isEditable);
            companyPhoneTextField.setEditable(isEditable);
            companyRegonTextField.setEditable(isEditable);
            companyZipCodeTextField.setEditable(isEditable);
    }
    
    protected String getString(String key){
        return StringsProperties.getString(key);
    }
        
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        companyDetailsPanel = new javax.swing.JPanel();
        companyNameLabel = new javax.swing.JLabel();
        companyNameTextField = new javax.swing.JTextField();
        companyAddressLabel = new javax.swing.JLabel();
        companyAddressTextField = new javax.swing.JTextField();
        companyZipCodeLabel = new javax.swing.JLabel();
        companyZipCodeTextField = new javax.swing.JTextField();
        companyCityLabel = new javax.swing.JLabel();
        companyCityTextField = new javax.swing.JTextField();
        companyPhoneLabel = new javax.swing.JLabel();
        companyPhoneTextField = new javax.swing.JTextField();
        companyFaxLabel = new javax.swing.JLabel();
        companyFaxTextField = new javax.swing.JTextField();
        companyEmailLabel = new javax.swing.JLabel();
        companyEmailTextField = new javax.swing.JTextField();
        companyNipLabel = new javax.swing.JLabel();
        companyNipTextField = new javax.swing.JTextField();
        companyRegonLabel = new javax.swing.JLabel();
        companyRegonTextField = new javax.swing.JTextField();

        companyDetailsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(getString("company_details")));

        companyNameLabel.setText(getString("company_name"));

        companyAddressLabel.setText(getString("company_address"));

        companyZipCodeLabel.setText(getString("company_zip_code"));

        companyCityLabel.setText(getString("company_city"));

        companyPhoneLabel.setText(getString("company_phone"));

        companyFaxLabel.setText(getString("company_fax"));

        companyEmailLabel.setText(getString("company_email"));

        companyNipLabel.setText(getString("company_nip"));

        companyRegonLabel.setText(getString("company_regon"));

        javax.swing.GroupLayout companyDetailsPanelLayout = new javax.swing.GroupLayout(companyDetailsPanel);
        companyDetailsPanel.setLayout(companyDetailsPanelLayout);
        companyDetailsPanelLayout.setHorizontalGroup(
            companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyDetailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(companyNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(companyPhoneLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(companyAddressLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(companyZipCodeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(companyFaxLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(companyEmailLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(companyNipLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(companyRegonLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(companyRegonTextField)
                    .addComponent(companyNipTextField)
                    .addComponent(companyEmailTextField)
                    .addComponent(companyFaxTextField)
                    .addComponent(companyNameTextField)
                    .addGroup(companyDetailsPanelLayout.createSequentialGroup()
                        .addComponent(companyZipCodeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                        .addComponent(companyCityLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(companyCityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(companyAddressTextField)
                    .addComponent(companyPhoneTextField, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        companyDetailsPanelLayout.setVerticalGroup(
            companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyDetailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(companyNameLabel)
                    .addComponent(companyNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(companyAddressLabel)
                    .addComponent(companyAddressTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(companyZipCodeLabel)
                    .addComponent(companyZipCodeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(companyCityLabel)
                    .addComponent(companyCityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(companyPhoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(companyPhoneLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(companyFaxLabel)
                    .addComponent(companyFaxTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(companyEmailLabel)
                    .addComponent(companyEmailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(companyNipLabel)
                    .addComponent(companyNipTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(companyDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(companyRegonLabel)
                    .addComponent(companyRegonTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(companyDetailsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(companyDetailsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel companyAddressLabel;
    private javax.swing.JTextField companyAddressTextField;
    private javax.swing.JLabel companyCityLabel;
    private javax.swing.JTextField companyCityTextField;
    private javax.swing.JPanel companyDetailsPanel;
    private javax.swing.JLabel companyEmailLabel;
    private javax.swing.JTextField companyEmailTextField;
    private javax.swing.JLabel companyFaxLabel;
    private javax.swing.JTextField companyFaxTextField;
    private javax.swing.JLabel companyNameLabel;
    private javax.swing.JTextField companyNameTextField;
    private javax.swing.JLabel companyNipLabel;
    private javax.swing.JTextField companyNipTextField;
    private javax.swing.JLabel companyPhoneLabel;
    private javax.swing.JTextField companyPhoneTextField;
    private javax.swing.JLabel companyRegonLabel;
    private javax.swing.JTextField companyRegonTextField;
    private javax.swing.JLabel companyZipCodeLabel;
    private javax.swing.JTextField companyZipCodeTextField;
    // End of variables declaration//GEN-END:variables
}
